package com.baor.vip.service.impl;


import com.baor.common.core.domain.R;
import com.baor.vip.domain.*;
import com.baor.vip.mapper.*;
import com.baor.vip.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * 会员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-17
 */
@Service
public class VipServiceImpl implements VipService {
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private BlackMapper blackMapper;
    @Autowired
    private BrokerMapper brokerMapper;
    @Autowired
    private ScoreMapper scoreMapper;


    /**
     * 查询会员
     *
     * @param vipUid 会员主键
     * @return 会员
     */
    @Override
    public R selectTVipByVipUid(Integer vipUid) {

        // 佣金
        VipInfoDTO brokerVO = brokerMapper.vipBroker(vipUid);
        if (brokerVO == null) {
            brokerVO = new VipInfoDTO();
            brokerVO.setVipBroker(0);
            brokerVO.setBrokerAccrued(0);
        }
        // 余额
        VipInfoDTO blackVO = blackMapper.vipBlack(vipUid);
        if (blackVO == null) {
            blackVO = new VipInfoDTO();
            blackVO.setVipBlack(0);
            blackVO.setBlackAccrued(0);
        }
        // 积分
        VipInfoDTO scoreVO = scoreMapper.vipScore(vipUid);
        if (scoreVO == null) {
            scoreVO = new VipInfoDTO();
            scoreVO.setVipScore(0);
            scoreVO.setScoreAccrued(0);
        }
        // 会员信息
        VipVO vipVO = vipMapper.selectVipVOByVipUid(vipUid);
//        if (vipVO == null) {
//            throw new RuntimeException("会员不存在");
//        }

        VipInfoDTO vipAssetsDTO = new VipInfoDTO();
        vipAssetsDTO.setVipBroker(brokerVO.getVipBroker() != null ? brokerVO.getVipBroker() : 0);
        vipAssetsDTO.setBrokerAccrued(brokerVO.getBrokerAccrued() != null ? brokerVO.getBrokerAccrued() : 0);
        vipAssetsDTO.setVipBlack(blackVO.getVipBlack() != null ? blackVO.getVipBlack() : 0);
        vipAssetsDTO.setBlackAccrued(blackVO.getBlackAccrued() != null ? blackVO.getBlackAccrued() : 0);
        vipAssetsDTO.setVipScore(scoreVO.getVipScore() != null ? scoreVO.getVipScore() : 0);
        vipAssetsDTO.setScoreAccrued(scoreVO.getScoreAccrued() != null ? scoreVO.getScoreAccrued() : 0);

        vipAssetsDTO.setVipUname(vipVO.getVipUname());
        vipAssetsDTO.setVipIcon(vipVO.getVipIcon());
        vipAssetsDTO.setVipPhone(vipVO.getVipPhone());
        vipAssetsDTO.setVipBir(vipVO.getVipBir());
        vipAssetsDTO.setVipSex(vipVO.getVipSex());
        vipAssetsDTO.setVipUnionid(vipVO.getVipUnionid());
        vipAssetsDTO.setVipOpenid(vipVO.getVipOpenid());
        vipAssetsDTO.setVipOrigin(vipVO.getVipOrigin());
        vipAssetsDTO.setCreatTime(vipVO.getCreatTime());
        vipAssetsDTO.setQuitTime(vipVO.getQuitTime());
        vipAssetsDTO.setTagName(vipVO.getTagName());
        vipAssetsDTO.setTagId(vipVO.getTagId());
        return R.ok(vipAssetsDTO);
    }

    /**
     * 查询会员列表
     *
     * @param tVip 会员
     * @return 会员
     */
    @Override
    public List<VipVO> selectTVipList(VipVO tVip)
    {
        System.out.println("cans"+tVip.getVipOrigin());
        return vipMapper.selectVipVOList(tVip);
    }


    /**
     * 新增会员
     * 
     * @param tVip 会员
     * @return 结果
     */
    @Override
    public R insertVipVO(VipVO tVip) {
        //判空
        if(tVip.getVipUname() == null && tVip.getVipUname().isEmpty()){
            return R.fail("会员名不能为空");
        }
        if(tVip.getVipPhone() == null && tVip.getVipPhone().isEmpty()){
            return R.fail("会员手机号不能为空");
        }
        if(tVip.getVipPassword() == null && tVip.getVipPassword().isEmpty()){
            return R.fail("密码不能为空");
        }
        //用户编号
        String vipNum = "NUM"+String.format("%05d", new Random().nextInt(100000));
        tVip.setVipNum(vipNum);
        //账号
        String vipAccount = String.format("%04d", new Random().nextInt(10000));
        tVip.setVipAccount(vipAccount);
        //验重
        if(vipMapper.phoneRepeat(tVip.getVipPhone(),null) > 0 ){
           return R.fail("手机号已存在");
        }
        //加密
        String password = tVip.getVipPassword();
        tVip.setVipPassword(new BCryptPasswordEncoder().encode(password));
        return R.ok(vipMapper.insertVipVO(tVip));
    }

    /**
     * 修改会员
     * 
     * @param tVip 会员
     * @return 结果
     */
    @Override
    public R updateVipVO(VipVO tVip) {
        //验重
        if(vipMapper.phoneRepeat(tVip.getVipPhone(),tVip.getVipUid()) > 0 ){
            return R.fail("手机号已存在");
        }
        return R.ok(vipMapper.updateVipVO(tVip));
    }

    /**
     * 批量删除会员
     * 
     * @param vipUids 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteVipVOByVipUids(Integer[] vipUids)
    {
        return vipMapper.deleteVipVOByVipUids(vipUids);
    }

    /**
     * 删除会员信息
     * 
     * @param vipUid 会员主键
     * @return 结果
     */
    @Override
    public R deleteVipVOByVipUid(Integer vipUid)
    {
        return R.ok(vipMapper.deleteVipVOByVipUid(vipUid));
    }

    //会员标签下拉框
    @Override
    public List<TagVO> selectTagList() {
        return tagMapper.tagBox();
    }

    //会员来源下拉框
    @Override
    public List<String> vipOriginBox() {
        return vipMapper.vipOriginBox();
    }

    @Override
    public VipVO userInfo(Integer vipUid) {
        return vipMapper.userInfo(vipUid);
    }

//


}
